package com.qspda.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qs.utils.PrintUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

public class UpdateActivity extends Activity {

	private EditText menage_et;

	private TextView tv_download_state;
	
	private ProgressBar pb_download = null;
	
	ArrayList<byte[]> list = new ArrayList<byte[]>();

//	int arraysNum = 1000;
	int arraysNum = 128;
	
	int barNum=0;

	NumberFormat numberFormat = NumberFormat.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update);

		menage_et = (EditText) findViewById(R.id.menage_et);
		
		pb_download=(ProgressBar) findViewById(R.id.pb_download);
		
		tv_download_state=(TextView) findViewById(R.id.tv_download_state);
		
		findViewById(R.id.update).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				menage_et.setText("");
				// if (updateFilePath1 == null) {
				pb_download.setProgress(0);
				tv_download_state.setText("选择");
				showIsUpdateDialog();
			}
		});

		// 设置精确到小数点后0位
		numberFormat.setMaximumFractionDigits(0);

	}

	private void showIsUpdateDialog() {
		new AlertDialog.Builder(UpdateActivity.this)
				.setTitle("更新")
				// .setMessage("Update file Path：" + updateFilePath)
				.setPositiveButton("更新",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								// TODO Auto-generated method stub
								showMessage("更新");
								new UpdateThread().execute();
							}
						}).setNegativeButton("取消", null).show();
	}

//	public static byte[] readFile(String file) throws IOException {
//		InputStream is = new FileInputStream(file);
//		int length = is.available();
//		byte bt[] = new byte[length];
//		is.read(bt);
//		is.close();
//		return bt;
//	}
	
	public byte[] readFile() throws IOException {
		Resources resources = this.getResources();
		InputStream is = null;
		is = resources.openRawResource(R.raw.pt58_601_9031);
		byte buffer[] = new byte[is.available()];
		is.read(buffer);
		is.close();
		return buffer;
	}

	class UpdateThread extends AsyncTask<File, Integer, Integer> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// ProgressDialogUtils.showProgressDialog(PosUpdateActivity.this,
			// "正在升级...");
		}

		@Override
		protected Integer doInBackground(File... params) {
			// TODO Auto-generated method stub
			try {
				
				//读出升级文件的全部字节
				byte[] bt1 = readFile();
				//校验和 checksum 
				byte[] bt2=sumMultiIntArray(bt1);
				//长度
				byte[] leng=toLH(bt1.length);
				//前缀
				byte[] bt3=new byte[]{0x1b,0x23,0x23,0x55,0x50,0x50,0x47};
				//前缀加校验和
				byte[] bt4=concat(bt3, bt2);
				//长度len拼接具体数据
				byte[] bt5=concat(leng, bt1);
				//bt4拼接bt5,即最终数组
				byte[] bt=concat(bt4, bt5);
				
				for (int i = 0; i < (bt.length / arraysNum + 1); i++) {
					if (arraysNum * (i + 1) >= bt.length) {
						list.add(Arrays.copyOfRange(bt, arraysNum * i,
								bt.length));
					} else {
						list.add(Arrays.copyOfRange(bt, arraysNum * i,
								arraysNum * (i + 1)));
					}
				}
				
				barNum=list.size();
				
				//启动发送线程
				handler.postDelayed(mRun_start, 1000);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}

		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}
	}

	int sendNum = 0;
	Handler handler = new Handler();
	Runnable mRun_start = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			if(sendNum>=barNum){
				
				sendNum=0;
				//发送更新指令至打印机
				
				handler.removeCallbacks(mRun_start);
				
				tv_download_state.setText("升级成功");
				
				pb_download.setProgress(100);
				
			}else{
				try {
					//发送更新指令至打印机
					PrintUtils.send1(list.get(sendNum));
				} catch (Exception e) {
					tv_download_state.setText("升级异常");
					showMessage(e.toString());
					e.printStackTrace();
					return;
				}
				
				String result = numberFormat.format((float) sendNum / (float) barNum * 100+1);
				tv_download_state.setText(result+"%");
				pb_download.setProgress(Integer.valueOf(result));
				
				sendNum++;
				
				handler.postDelayed(mRun_start, 10);
				
			}
		}
	};

	static byte[] concat(byte[] a, byte[] b) {  
		byte[] c= new byte[a.length+b.length];  
		   System.arraycopy(a, 0, c, 0, a.length);  
		   System.arraycopy(b, 0, c, a.length, b.length);  
		   return c;  
		}  

	/**
	 * 消息展示
	 */
	public void showMessage(String str) {
		String str1 = menage_et.getText().toString();
		menage_et.setText(str1 + "\n" + str);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	/**
	 * 整数转成四字节
	 * @param i
	 * @return
	 */
	   public static byte[] intToByte4(int i) {  
	        byte[] targets = new byte[4];  
	        targets[3] = (byte) (i & 0xFF);  
	        targets[2] = (byte) (i >> 8 & 0xFF);  
	        targets[1] = (byte) (i >> 16 & 0xFF);  
	        targets[0] = (byte) (i >> 24 & 0xFF);  
	        return targets;  
	    }  

	/**
	* 将int转为大端，低字节存储高位
	* 
	* @param n
	* int
	* @return byte[]
	*/
	public static byte[] toHH(int n) {
	    byte[] b = new byte[4];
	    b[3] = (byte) (n & 0xff);
	    b[2] = (byte) (n >> 8 & 0xff);
	    b[1] = (byte) (n >> 16 & 0xff);
	    b[0] = (byte) (n >> 24 & 0xff);
	    return b;
	}  
	
	/**
	 * 小端模式 将int转为低字节在前，高字节在后的byte数组
	 * 
	 * @param n
	 *            int
	 * @return byte[]
	 */
	public static byte[] toLH(int n) {
		byte[] b = new byte[4];
		b[0] = (byte) (n & 0xff);
		b[1] = (byte) (n >> 8 & 0xff);
		b[2] = (byte) (n >> 16 & 0xff);
		b[3] = (byte) (n >> 24 & 0xff);
		return b;
	}
	
	
	/**
	 * 合并数组
	 * 
	 * @param data1
	 * @param data2
	 * @return
	 */
	public byte[] twoToOne(byte[] data1, byte[] data2) {

		byte[] data3 = new byte[data1.length + data2.length];
		System.arraycopy(data1, 0, data3, 0, data1.length);
		System.arraycopy(data2, 0, data3, data1.length, data2.length);
		return data3;

	}
	
	
	/**
	 * 数组累加
	 * @param bt
	 * @return
	 */
	public static byte[] sumMultiIntArray(byte[] bt){

		int fileByte=0;        //做累加
		for (byte b:bt) { 
		    fileByte+=b&0xff;  //b&0xff 就是将字节转为无符号类型的 
		}

		byte[] loB4=intToBytes(fileByte);  //checksum 取低四字节
		
		return loB4;
    }

	/**
	 * 将int数值转换为占四个字节的byte数组
	 * @param value
	 *            要转换的int值
	 * @return byte数组
	 */
	public static byte[] intToBytes( int value )
	{
	    byte[] src = new byte[4];
	    src[3] =  (byte) ((value>>24) & 0xFF);
	    src[2] =  (byte) ((value>>16) & 0xFF);
	    src[1] =  (byte) ((value>>8) & 0xFF);
	    src[0] =  (byte) (value & 0xFF);
	    return src;
	}
	
}
